home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / gamgraph / sier10.zip / SIER.C < prev    next >
C/C++ Source or Header  |  1994-08-03  |  3KB  |  142 lines

  1. /****************** Sierpinsky Gasket, alias The Chaos Game ****************/
  2. /*                                                                         */
  3. /*             M\Cooper, Route 1, Box 204, Grantsville, MD 21536           */
  4. /*                             thegrendel@aol.com                          */
  5. /*                                                                         */
  6. /***************************************************************************/
  7.  
  8. #include <conio.h>
  9. #include <graphics.h>
  10. #include <stdlib.h>
  11. #include <time.h>
  12.  
  13. #define NUMBER_OF_VERTICES 3
  14. #define POINT_COUNT (NUMBER_OF_VERTICES + 1)
  15. #define CIRCLE_RADIUS 4
  16. #define TRIANGLE_COLOR RED
  17. #define CIRCLE_COLOR CYAN
  18. #define BACKGROUND_COLOR WHITE
  19. #define MAX_SCREEN_WIDTH 639
  20. #define MAX_SCREEN_HEIGHT 479
  21. #define MID_DIVISOR 2
  22. #define V0x 320
  23. #define V0y 10
  24. #define V1x 10
  25. #define V1y 450
  26. #define V2x 630
  27. #define V2y 450
  28.  
  29.  
  30.    typedef struct { int x_coord, y_coord; } Point;
  31.    int triangle[ POINT_COUNT * 2 ] =
  32.                  { V0x, V0y, V1x, V1y, V2x, V2y, V0x, V0y };
  33.    Point V[NUMBER_OF_VERTICES] = {
  34.                  { V0x, V0y },
  35.                  { V1x, V1y },
  36.                  { V2x, V2y }
  37.                  };
  38.  
  39.    void graphics_setup( int background_color );
  40.    void draw_triangle();
  41.    void set_up();
  42.    void Sierpinsky();
  43.    Point get_seed();
  44.    Point calculate_midpoint( Point p1, Point p2 );
  45.  
  46.  
  47. void main()
  48. {
  49.   
  50.       set_up();
  51.      
  52.       Sierpinsky();
  53.  
  54.       getch(); // Stop the action...
  55.       getch(); // Wait for new keypress.
  56.       closegraph();
  57. }        
  58.  
  59. void set_up()
  60. {
  61.  
  62.       randomize();
  63.       graphics_setup( BACKGROUND_COLOR );
  64.       setlinestyle( SOLID_LINE, 1, NORM_WIDTH );
  65.       draw_triangle();
  66.  
  67.       return;
  68.  
  69. }
  70.  
  71. void graphics_setup( int background_color )
  72. {
  73.    int grdriver = VGA,
  74.        grmode = VGAHI;
  75.  
  76.        registerfarbgidriver( EGAVGA_driver_far );
  77.        registerfarbgifont( triplex_font_far );
  78.        initgraph( &grdriver, &grmode, "" );
  79.        setbkcolor( background_color );
  80.  
  81.        return;
  82.  
  83. }
  84.  
  85. void draw_triangle( )
  86. {
  87.  
  88.       setcolor( TRIANGLE_COLOR );
  89.       drawpoly( POINT_COUNT, triangle );
  90.  
  91.       return;
  92. }
  93.  
  94. Point get_seed()
  95. {
  96.    Point Initial;
  97.  
  98.    Initial.x_coord = random( MAX_SCREEN_WIDTH );
  99.    Initial.y_coord = random( MAX_SCREEN_HEIGHT ); 
  100.  
  101.       return ( Initial );
  102. }
  103.  
  104. Point calculate_midpoint( Point p1, Point p2 )
  105. {
  106.    Point mid;
  107.  
  108.       mid.x_coord = ( p2.x_coord + p1.x_coord ) / MID_DIVISOR;
  109.  
  110.       mid.y_coord = ( p2.y_coord + p1.y_coord ) / MID_DIVISOR;
  111.  
  112.       return( mid );
  113.  
  114.  
  115. void Sierpinsky()
  116. {
  117.    Point Pi,
  118.       P,
  119.       Pnext;
  120.     int pixel_color;
  121.     register int t;
  122.  
  123.       Pi = get_seed();
  124.       P = Pi;
  125.       putpixel( P.x_coord, P.y_coord, pixel_color );
  126.       setcolor( CIRCLE_COLOR );
  127.       circle( P.x_coord, P.y_coord, CIRCLE_RADIUS );
  128.  
  129.       while( !kbhit() )
  130.      {
  131.      t = random( NUMBER_OF_VERTICES );
  132.  
  133.      Pnext = calculate_midpoint( P, V[t] );
  134.      putpixel( Pnext.x_coord, Pnext.y_coord, 2 * t + 1 );
  135.      P = Pnext;
  136.      }
  137.  
  138.       return;
  139.  
  140. }
  141.